<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书管理系统-注册</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {background-image:url("layuimini/images/bg.jpg");height:100%;width:100%;}
        #container{height:100%;width:100%;}
        input:-webkit-autofill {-webkit-box-shadow:inset 0 0 0 1000px #fff;background-color:transparent;}
        .admin-login-background {width:300px;height:300px;position:absolute;left:50%;top:40%;margin-left:-150px;margin-top:-100px;}
        .admin-header {text-align:center;margin-bottom:20px;color:#ffffff;font-weight:bold;font-size:28px}
        i {position:absolute;}
    </style>
</head>
<body>
<div id="container">
    <div></div>
    <div class="admin-login-background">
        <div class="admin-header">
            <span>图书管理系统-注册账号</span>
        </div>
        <form class="layui-form" action="">
            <!--用户名-->
            <div>
                <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input" value="">
            </div>
            <!--密码-->
            <div>
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input" value="">
            </div>
            <!--确认密码-->
            <div>
                <input type="password" name="repetition" placeholder="请确认密码" autocomplete="off" class="layui-input" value="">
            </div>
            <div align="center" style="margin-top: 15px">
                <!--注册按钮-->
                <button class="layui-btn" lay-submit="" lay-filter="register">确认注册</button>
                <!--注册按钮-->
                <button class="layui-btn" id="btn_login" type="button">返回登录</button>

            </div>

        </form>
    </div>
</div>
<script src="./layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        $("#btn_login").click(function () {
            location.href="index.html";
        });

        // 登录过期的时候，跳出ifram框架
        if (top.location !== self.location) top.location = self.location;

        // 进行登录操作
        form.on('submit(register)', function (data) {
            data = data.field;
            if (data.username === '') {
                layer.msg('用户名不能为空');
                return false;
            }
            if (data.password === '') {
                layer.msg('密码不能为空');
                return false;
            }
            if (data.password === '') {
                layer.msg('确认密码不能为空');
                return false;
            }
            if(data.password !== data.repetition) {
                layer.msg('两次输入的密码不一致');
                return false;
            }

            $.ajax({
                url: "user/register",
                data: {
                    'username':data.username,
                    'password':data.password,
                    'authority':data.authority,
                },
                type: 'post',
                datatype: 'json',
                success: function (res) {
                    // return: 0 用户名重复，1登录成功
                    switch (res) {
                        case 0:
                            layer.msg("该用户名已存在!",{time:1000});
                            break;
                        case 1: //登录成功
                            layer.msg("注册成功",{time:1000},function () {
                                // 注册成功后跳转到登录界面
                                location.href="index.html";
                            });
                            break;
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>